package com.example.book_shop_java.controller;

import com.alibaba.fastjson.JSON;
import com.example.book_shop_java.bean.Notices;
import com.example.book_shop_java.dao.NoticesDao;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class NoticesController {
    @Resource
    NoticesDao noticesDao;

    @RequestMapping("/getAllNotices")
    public String getAllNotices(@RequestParam("context") String context){
        List<Notices> notices = noticesDao.getAllNotices("%"+context+"%");
        String message = "error";
        int code = 413;

        if(notices.size()>0){
            message = "success";
            code = 200;
        }
        HashMap<String ,Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);
        res.put("allNotices",notices);

        String allNoticesMessage = JSON.toJSONString(res);
        return allNoticesMessage;
    }

    @RequestMapping("/deleteNotices")
    public String deleteNotices(int id){
        int i = noticesDao.deleteNotices(id);
        String message = "error";
        int code = 413;
        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String ,Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);

        String deleteNoticesMessage = JSON.toJSONString(res);
        return deleteNoticesMessage;
    }

    @RequestMapping("/editNotices")
    public String editNotices(int id,
                              @RequestParam("topic") String topic,
                              @RequestParam("context") String context
    ){
        int i = noticesDao.editNotices(id,topic,context);
        String message = "error";
        int code = 413;
        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String ,Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);

        String editNoticesMessage = JSON.toJSONString(res);
        return editNoticesMessage;
    }

    @RequestMapping("/addNotices")
    public String addNotices(@RequestParam("topic") String topic,
                             @RequestParam("username") String username,
                             @RequestParam("timeOut") String timeOut,
                             @RequestParam("context") String context){
        int i = noticesDao.addNotices(topic,username,timeOut,context);
        String message = "error";
        int code = 413;
        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String ,Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);

        String addNoticesMessage = JSON.toJSONString(res);
        return addNoticesMessage;
    }
}
